'use strict';

require('../../assets/scss/entry/common.scss');
require('../../assets/scss/entry/agency.scss');

let $ = jQuery = window.jQuery = window.$ = require('jquery');

import * as api from '../../components/api/index.js';
import url from '../../components/util/url.js';
import util from '../../components/util/util.js';

let pageEntry = (function() {

	return {
		initCheck() {
			let nowUrl = window.location.href;
			util.initLogin(nowUrl,"1");
		},
		initExamInfo() {
			let agencyCode = url.getQuery('agencyCode');

			if(!(util.isEmpty(agencyCode))){
				$('#J_agency_code').html(agencyCode);

				api.agencyExam(agencyCode).then(resp => {
					if (resp.ret == "0") {
						if (resp.content.examPassNo == 0) {
							$('#J_exam_pass_no').html('无');
						}
						else {
							$('#J_exam_pass_no').html(resp.content.examPassNo + '人');

							if (resp.content.examPassUserNames && resp.content.examPassUserNames.length > 0) {
								for (let i = 0, len = resp.content.examPassUserNames.length; i < len; i++) {
									let $span = $('<span class="user-item">'+ resp.content.examPassUserNames[i] +'</span>');
									$('#J_exam_pass_names').append($span);
								}
							}
						}

						if (resp.content.examingNo == 0) {
							$('#J_examing_no').html('无');
						}
						else {
							$('#J_examing_no').html(resp.content.examingNo + '人');
						}
					}
				});
			}
		},
		bindEvent() {
			// 开始考试按钮事件
			$('#J_start_exam').on('click', function() {
				api.getExamInfo($.cookie('userCode'), $("#studentName").val(), $("#examCode").val()).then((resp) => {
					let examType = 0;

					if (resp.ret == '0') {
						// 获得已完成题数
						// let currentExam = resp.content.paper.examQuestionIndex;
						let currentExam = "0";
						// 获得应该进入题目的模板
						examType = resp.content.paper.questions[currentExam].answerWay;

						if (examType == 1) {//音频
							window.location.href = '/examAudio.html?userName=' + $("#studentName").val() + '&examCode=' + $("#examCode").val();
						}
						else if (examType == 2) {//视频
							window.location.href = '/examVideo.html?userName=' + $("#studentName").val() + '&examCode=' + $("#examCode").val();
						}
					}
				});
			});
		}
	}
})();

pageEntry.initCheck();
pageEntry.initExamInfo();
pageEntry.bindEvent();
